import { userInfoUrl } from '../../../../api/individual'
import { post } from '../../../../utils/index'

Page({
  data: {
    active: 1,
    info: {},
    version: 1.0
  },
  async loadData () { // 获取用户信息
    const { data } = await post(userInfoUrl)
    data.encryptPhone = data.phone.replace(/^(\d{4})\d{4}(\d+)/, "$1****$2")
    this.setData({ info: data })
    this.updateUserStatus()
  },
  updateUserStatus () { // 更新用户状态
    const userStatus = wx.getStorageSync('userStatus')
    userStatus.status = this.data.info.status
    wx.setStorageSync('userStatus', userStatus)
  },
  onChange ({ detail }) { // 切换路由
    if (detail === 0) {
      wx.redirectTo({ url: '/pages/individual/work/home/home' })
    }
  },
  loginout () { // 退出登录
    wx.clearStorageSync()
    wx.reLaunch({ url: '/pages/login/login' })
  },
  getVersion () { // 获取小程序版本号
    const { miniProgram } = wx.getAccountInfoSync()
    const { version = 1.0, } = miniProgram
    this.setData(version)
  },
  onLoad () {
    this.loadData()
    this.getVersion()
  },
  onPullDownRefresh () {
    this.loadData()
  }
})